﻿using System.Collections.Generic;
using UnityEngine;

public class MainUIEngine : MonoBehaviour
{
    [SerializeField] List<GameObject> images = new List<GameObject>();          //所有游戏素材
    private GameObject gameZone;                                                //游戏区域
    public BaseObjCtrler baseObj;
    private GameObject firstObj;                                              //第一次点击对象
    private GameObject twoObj;                                                //第二次点击对象

    private void Awake()
    {
        gameZone = GameObject.Find("GameZone");
        generrateImageObjects();
        disturb();
    }

    private void Update()
    {
        
    }


    /// <summary>
    /// 摆放游戏对象
    /// </summary>
    private void generrateImageObjects()
    {
        //生成次数
        int num = 24;
        for (int i = 0; i < num; i++)
        {
            int ranNum = Random.Range(0, 7);
            GameObject gameObject1 = Instantiate(images[ranNum], gameZone.transform);
            gameObject1.name = images[ranNum].name;
            GameObj gameObj1 = gameObject1.GetComponent<GameObj>();
            gameObj1.initGameObj(this);

            GameObject copyObject1 = Instantiate(images[ranNum], gameZone.transform);
            copyObject1.name = images[ranNum].name;
            GameObj copyObj1 = copyObject1.GetComponent<GameObj>();
            copyObj1.initGameObj(this);
        }
    }

    /// <summary>
    /// 打乱对象顺序
    /// </summary>
    private void disturb()
    {
        int disturbNum = 50;
        Transform[] items = gameZone.GetComponentsInChildren<Transform>();
        for (int i = 0; i < disturbNum; i++)
        {
            int firstRandom = Random.Range(0, items.Length);
            int twoRandom = Random.Range(0, items.Length);
            //SetSiblingIndex 用于控制同级顺序
            items[firstRandom].transform.SetSiblingIndex(twoRandom);
        }
    }
}
